
package GUI;

import java.awt.*;

import javax.swing.*;

import Entity.Customer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * @author aviat
 *
 */
public class UpdateCustomerDetails extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JTextField lNametextField;

	private JLabel lblGLastName;
	private JLabel lblBFirstName;
	private JLabel lblBLastName;
	private JComboBox<String> bLnamecomboBox;
	

	private JLabel iconlabel;
	private JLabel lblGFirstName;
	private JLabel lblCustomerId;
	private JComboBox<String> CustomerIDcomboBox;
	private JLabel lblAddress;
	private JButton btnContinue;
	private JComboBox<String> gFnameComboBox;
	private JComboBox<String> gLnameComboBox;
	private JComboBox<String> bFnameComboBox;
	private JButton btnRefresh;
	
	private ArrayList<Customer> c_allDate;
	private ArrayList<Customer> c_filteredDate;
	private Object syncObject;
	private boolean c_isChangeAllowed;
	private Customer customer;
	private JComboBox<String> AddresscomboBox;
	

	
	public UpdateCustomerDetails() {
		setBackground(new Color(255, 51, 0));
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(UpdateCustomerDetails.class.getResource("/resources/back22.png")));
		lNametextField = new JTextField();
		lNametextField.setEditable(false);
		AddresscomboBox = new JComboBox<String>();
		AddresscomboBox.addActionListener(new ActionListener(){ 
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(AddresscomboBox.getSelectedItem().toString(),"ADDRESS");
					}
				}
			}
		});
		CustomerIDcomboBox = new JComboBox<String>();
		btnContinue = new JButton("Continue");
		btnRefresh = new JButton("refresh");
		gFnameComboBox = new JComboBox<String>();
		bFnameComboBox = new JComboBox<String>();
		gLnameComboBox = new JComboBox<String>();
		bLnamecomboBox = new JComboBox<String>();

		initialize();
	}

	private void initialize() {


		JLabel lblCustomerDetails = new JLabel("Update Customer Details");
		lblCustomerDetails.setForeground(new Color(0, 204, 255));
		lblCustomerDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblCustomerDetails.setBounds(10, 11, 211, 44);
		add(lblCustomerDetails);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		

		lblGLastName = new JLabel("Groom Last Name");
		lblGLastName.setForeground(Color.WHITE);
		lblGLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGLastName.setBounds(10, 164, 111, 22);
		add(lblGLastName);
		
		lblBFirstName = new JLabel("Bride First Name");
		lblBFirstName.setForeground(Color.WHITE);
		lblBFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBFirstName.setBounds(10, 232, 111, 22);
		add(lblBFirstName);
		
		lblBLastName = new JLabel("Bride Last Name");
		lblBLastName.setForeground(Color.WHITE);
		lblBLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBLastName.setBounds(10, 300, 101, 22);
		add(lblBLastName);

		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblGFirstName = new JLabel("Groom First Name");
		lblGFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGFirstName.setForeground(Color.WHITE);
		lblGFirstName.setBounds(10, 92, 127, 26);
		add(lblGFirstName);
		
		lblCustomerId = new JLabel("Customer ID");
		lblCustomerId.setForeground(Color.WHITE);
		lblCustomerId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCustomerId.setBounds(348, 238, 82, 22);
		add(lblCustomerId);
		
		
		CustomerIDcomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(CustomerIDcomboBox.getSelectedItem().toString(),"CUSTOMERID");
					}
				}
			}
		});
		CustomerIDcomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		CustomerIDcomboBox.setBounds(499, 238, 135, 27);
		add(CustomerIDcomboBox);
		
		lblAddress = new JLabel("Address");
		lblAddress.setForeground(Color.WHITE);
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAddress.setBounds(361, 98, 82, 21);
		add(lblAddress);
		
		
		btnContinue.setIcon(new ImageIcon(UpdateCustomerDetails.class.getResource("/resources/continueArrow.png")));
		btnContinue.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnContinue.setBounds(386, 407, 135, 27);
		btnContinue.setEnabled(false);
		add(btnContinue);
		
		
		gFnameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(gFnameComboBox.getSelectedItem().toString(),"GFNAME");
					}
				}
			}
		});
		gFnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		gFnameComboBox.setBounds(161, 92, 135, 27);
		add(gFnameComboBox);
		
		
		gLnameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(gLnameComboBox.getSelectedItem().toString(),"GLNAME");
					}
				}
			}
		});
		gLnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		gLnameComboBox.setBounds(161, 166, 135, 27);
		add(gLnameComboBox);
		
		
		bLnamecomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(bLnamecomboBox.getSelectedItem().toString(),"BLNAME");
					}
				}
			}
		});;
		bLnamecomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		bLnamecomboBox.setBounds(161, 300, 135, 27);
		add(bLnamecomboBox);
		
		
		bFnameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(bFnameComboBox.getSelectedItem().toString(),"BFNAME");
					}
				}
			}
		});
		bFnameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		bFnameComboBox.setBounds(161, 232, 135, 27);
		add(bFnameComboBox);
		
		
		btnRefresh.setIcon(new ImageIcon(UpdateCustomerDetails.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						btnContinue.setEnabled(false);
						filterList(null,"ALL");
					}
				}
			}
		});
		btnRefresh.setBounds(233, 407, 118, 27);
		add(btnRefresh);
		
		
		AddresscomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		AddresscomboBox.setBounds(499, 101, 180, 27);
		add(AddresscomboBox);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
	}

	public void setAllCustomer(ArrayList<Customer> list){
		c_allDate = list;
		c_filteredDate = new ArrayList<>();
		syncObject = new Object();
		filterList(null,"ALL");
		 
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			c_filteredDate.clear();
			
			switch(scope)
			{
			case "GFNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getgFname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "GLNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getgLname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "BFNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getbFname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "BLNAME":
				for (Customer c :c_allDate) 
				{
					if(c.getbLname().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "CUSTOMERID":
				for (Customer c :c_allDate) 
				{
					if(String.valueOf(c.getcID()).equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "ADDRESS":
				for (Customer c :c_allDate) 
				{
					if(c.getAddress().equals(selectedItem))
					{
						c_filteredDate.add(c);
					}
				}
				break;
			case "ALL":
				c_filteredDate.addAll(c_allDate);
				break;
			}
		}
		c_isChangeAllowed = false;
		filterComboBox(gFnameComboBox,"GFNAME");
		filterComboBox(gLnameComboBox,"GLNAME");
		filterComboBox(bFnameComboBox,"BFNAME");
		filterComboBox(bLnamecomboBox,"BLNAME");
		filterComboBox(CustomerIDcomboBox,"CUSTOMERID");
		filterComboBox(AddresscomboBox,"ADDRESS");
		
		if(gFnameComboBox.getItemCount()==1 && gLnameComboBox.getItemCount()==1 && bFnameComboBox.getItemCount()==1 && 
				bLnamecomboBox.getItemCount()==1 && CustomerIDcomboBox.getItemCount()==1&& AddresscomboBox.getItemCount()==1 )
		{
			setCustDetails();
		}
		c_isChangeAllowed = true;

	}
	/**
	* This method adding items to combobox
	* @param combo is the combobox to add item to
	* @param scope is the key code for adding the correct field 
	* 
	*/
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject)
		{
			for(Customer c: c_filteredDate )
			{
				switch(scope)
				{
				case "GFNAME":
					combo.addItem(c.getgFname());
					break;
				case "GLNAME":
					combo.addItem(c.getgLname());
					break;
				case "BFNAME":
					combo.addItem(c.getbFname());
					break;
				case "BLNAME":
					combo.addItem(c.getbLname());
					break;
				case "CUSTOMERID":
					combo.addItem(String.valueOf(c.getcID()));
					break;
				case "ADDRESS":
					combo.addItem(c.getAddress());
					break;
				}
			}
		}
	}
	/**
	* @return the e_isChangeAllowed
	* 
	*/
	private boolean isChangeAllowed()
	{
		return c_isChangeAllowed;
	}
	/**
	 * This method add the customers details
	 */
	public void setCustDetails(){
		
		for(Customer c : c_filteredDate){
			
			if(c.getcID()==Integer.parseInt(CustomerIDcomboBox.getSelectedItem().toString()))
			{
				btnContinue.setEnabled(true);
				customer=c;
			}
			break;
		}
		
	}


	/**
	 * @return the customer
	 */
	public Customer getCustomer() {
		return customer;
	}

	/**
	 * @return the customerIDcomboBox
	 */
	public JComboBox<String> getCustomerIDcomboBox() {
		return CustomerIDcomboBox;
	}

	/**
	 * @param customerIDcomboBox the customerIDcomboBox to set
	 */
	public void setCustomerIDcomboBox(JComboBox<String> customerIDcomboBox) {
		CustomerIDcomboBox = customerIDcomboBox;
	}


	/**
	 * @return the bLnamecomboBox
	 */
	public JComboBox<String> getbLnamecomboBox() {
		return bLnamecomboBox;
	}

	/**
	 * @return the gFnameComboBox
	 */
	public JComboBox<String> getgFnameComboBox() {
		return gFnameComboBox;
	}

	/**
	 * @return the gLnameComboBox
	 */
	public JComboBox<String> getgLnameComboBox() {
		return gLnameComboBox;
	}

	/**
	 * @return the bFnameComboBox
	 */
	public JComboBox<String> getbFnameComboBox() {
		return bFnameComboBox;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the iconlabel
	 */
	public JLabel getIconlabel() {
		return iconlabel;
	}

	/**
	 * @param iconlabel the iconlabel to set
	 */
	public void setIconlabel(JLabel iconlabel) {
		this.iconlabel = iconlabel;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @return the c_allDate
	 */
	public ArrayList<Customer> getC_allDate() {
		return c_allDate;
	}

	/**
	 * @param c_allDate the c_allDate to set
	 */
	public void setC_allDate(ArrayList<Customer> c_allDate) {
		this.c_allDate = c_allDate;
	}

	/**
	 * @return the c_filteredDate
	 */
	public ArrayList<Customer> getC_filteredDate() {
		return c_filteredDate;
	}

	/**
	 * @param c_filteredDate the c_filteredDate to set
	 */
	public void setC_filteredDate(ArrayList<Customer> c_filteredDate) {
		this.c_filteredDate = c_filteredDate;
	}

	/**
	 * @return the c_isChangeAllowed
	 */
	public boolean isC_isChangeAllowed() {
		return c_isChangeAllowed;
	}

	/**
	 * @param c_isChangeAllowed the c_isChangeAllowed to set
	 */
	public void setC_isChangeAllowed(boolean c_isChangeAllowed) {
		this.c_isChangeAllowed = c_isChangeAllowed;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @param bLnamecomboBox the bLnamecomboBox to set
	 */
	public void setbLnamecomboBox(JComboBox<String> bLnamecomboBox) {
		this.bLnamecomboBox = bLnamecomboBox;
	}

	/**
	 * @param btnChange the btnChange to set
	 */
	public void setBtnChange(JButton btnChange) {
		this.btnContinue = btnChange;
	}

	/**
	 * @param gFnameComboBox the gFnameComboBox to set
	 */
	public void setgFnameComboBox(JComboBox<String> gFnameComboBox) {
		this.gFnameComboBox = gFnameComboBox;
	}

	/**
	 * @param gLnameComboBox the gLnameComboBox to set
	 */
	public void setgLnameComboBox(JComboBox<String> gLnameComboBox) {
		this.gLnameComboBox = gLnameComboBox;
	}

	/**
	 * @param bFnameComboBox the bFnameComboBox to set
	 */
	public void setbFnameComboBox(JComboBox<String> bFnameComboBox) {
		this.bFnameComboBox = bFnameComboBox;
	}

	/**
	 * @return the lNametextField
	 */
	public JTextField getlNametextField() {
		return lNametextField;
	}

	/**
	 * @param lNametextField the lNametextField to set
	 */
	public void setlNametextField(JTextField lNametextField) {
		this.lNametextField = lNametextField;
	}

	/**
	 * @return the addresscomboBox
	 */
	public JComboBox<String> getAddresscomboBox() {
		return AddresscomboBox;
	}

	/**
	 * @param addresscomboBox the addresscomboBox to set
	 */
	public void setAddresscomboBox(JComboBox<String> addresscomboBox) {
		AddresscomboBox = addresscomboBox;
	}

	/**
	 * @return the btnContinue
	 */
	public JButton getBtnContinue() {
		return btnContinue;
	}

	/**
	 * @param btnContinue the btnContinue to set
	 */
	public void setBtnContinue(JButton btnContinue) {
		this.btnContinue = btnContinue;
	}
	
}

